package com.ljh.onlinetest.controller.student;

import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.Subject;
import com.ljh.onlinetest.domain.User;
import com.ljh.onlinetest.service.SubjectService;
import com.ljh.onlinetest.viewmodel.student.education.SubjectVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @ClassName EducationController.java
 * @createTime 2022年03月09日
 */
@RestController("StudentEducationController")
@RequestMapping("/api/student/education")
public class EducationController extends BaseApiController {
    private final SubjectService subjectService;

    @Autowired
    public EducationController(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @PostMapping("/subject/list")
    public RestResponse<List<SubjectVM>> list(){
        User user = getCurrentUser();
        List<Subject> subjects = subjectService.getSubjectByLevel(user.getUserLevel());
        List<SubjectVM> subjectVMList = subjects.stream().map(subject -> {
            SubjectVM subjectVM = modelMapper.map(subject, SubjectVM.class);
            subjectVM.setId(String.valueOf(subject.getId()));
            return subjectVM;
        }).collect(Collectors.toList());
        return RestResponse.ok(subjectVMList);
    }
}
